<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 郑钟良<zzl@ourstu.com> <code-tech.diandian.com>
// +----------------------------------------------------------------------

namespace Question\Model;

use Think\Model;

/**
 * Class QuestionModel  问题模型
 * @package Question\Model
 * @author:郑钟良<zzl@ourstu.com>
 */
class QuestionModel extends Model
{

    protected $tableName = 'question';
    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('update_time', NOW_TIME, self::MODEL_UPDATE),
        array('status', '1', self::MODEL_INSERT),
    );
    protected $_validate = array(
        array('title', '4,20', '标题不能超过20个字', self::MUST_VALIDATE, 'length')
    );
    protected $insertField = 'title,content,answer_num,type,is_repost,last_question_id,first_question_id,share_num,repost_num,cover_id'; //新增数据时允许操作的字段
    protected $updateField = 'title,content,answer_num,type,is_repost,last_question_id,first_question_id,share_num,repost_num,cover_id'; //编辑数据时允许操作的字段

    /**编辑问题
     * @param array $data
     * @return bool
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function editQuestion($data = array())
    {
        $data = $this->create($data);
        if (!$data) return false;
        $result=$this->save($data);
        if($result){
            $result=$data['id'];
            S('question_'.$result,null);
        }
        return $result;
    }

    /**添加问题
     * @param array $data
     * @return bool
     * @author 郑钟良<zzl@ourstu.com>
     */
    public function createQuestion($data = array())
    {
        $data = $this->create($data);
        if (!$data) return false;
        return $this->add($data);
    }

    public function getList($map = array(), &$totalCount, $page = 1, $order = 'id desc', $r = 50)
    {
        $map['status'] = 1;
        $listIds = $this->where($map)->order($order)->page($page, $r)->field('id')->select();
        $totalCount = $this->where($map)->count();
        $list = array();
        foreach ($listIds as $val) {
            $list[] = $this->getQuestion($val['id']);
        }
        unset($val);
        return $list;
    }

    public function getQuestion($id)
    {
        $data = S('question_' . $id);
        if (!$data) {
            $data = $this->where(array('id' => $id))->find();
            $data['user'] = query_user(array('uid', 'nickname', 'space_url', 'avatar64', 'avatar128', 'rank_html', 'address', 'doing'), $data['uid']);
            $data['question_type'] = type_title($data['type']);
            if ($data['is_repost'] == 1) {
                $data['original_question'] = $this->getQuestion($data['first_question_id']);
                $data['content'] = closetags(parse_expression($data['content']));
            } else {
                if (mb_strlen(op_t($data['content']), 'utf-8') < 50 && !preg_match('/\<img/', $data['content'])) {
                    if(op_t($data['content'])==$data['content']){
                        $data['content_s'] = $data['content'];
                        $data['have_more'] = false;
                    }else{
                        $data['content_s'] = op_t($data['content']).'...';
                        $data['have_more'] = true;
                    }
                } else {
                    $data['content_s'] = msubstr(op_t($data['content']), 0, 50);
                    $data['have_more'] = true;
                }
            }
            S('question_' . $id, $data, 60);
        }
        return $data;
    }

    public function getIds($map = array())
    {
        $ids = $this->where($map)->field('id')->select();
        $ids = array_column($ids, 'id');
        return $ids;
    }

    public function addFieldNum($ids = array(), $field = 'answer_num')
    {
        if (count($ids) > 0) {
            $this->where(array('id' => array('in', $ids)))->setInc($field);
            foreach ($ids as $v) {
                S('question_' . $v, null);
            }
            unset($v);
        }
        return;
    }

    public function getUidById($id){
        $uid=$this->field('uid')->find($id);
        return $uid['uid'];
    }
}